package edu.esiag.isidis.fi.LBTest;



import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class ExistantsMembers implements Runnable {
   InetAddress  groupeIP;
   int port;
   String nom;
   MulticastSocket socketReception;
   
   private Set<String> list = new HashSet<String>();
   public Set<String> getList() {
	return list;
   }
public ExistantsMembers(InetAddress groupeIP, int port, String nom)  throws Exception { 
	   this.groupeIP = groupeIP;
	   this.port = port;
	   this.nom = nom;
	   socketReception = new MulticastSocket(port);
	   socketReception.joinGroup(groupeIP);
	     
  }

public void run() {
    DatagramPacket adresseMember;
    byte[] contenuMessage;
    String adresse;

    FileHandler fileHandler;
	  try {
		  fileHandler = new FileHandler("Khalid.log");

			final Logger logger = Logger.getLogger("toto");
			logger.addHandler( fileHandler);
			SimpleFormatter formatter = new SimpleFormatter();  
	        fileHandler.setFormatter(formatter);  
	        

    while(true) {
    	try{
    			contenuMessage = new byte[1024];
    			 adresseMember = new DatagramPacket(contenuMessage, contenuMessage.length);
	              socketReception.receive(adresseMember);
	              ByteArrayInputStream bis = new ByteArrayInputStream(contenuMessage);
	              ObjectInputStream ois = new ObjectInputStream(bis);
	              System.out.println(ois.readObject());
	       	   //System.out.println(list);
	       	logger.log(Level.INFO, "Annuaire Adresses " + list );
		  }
		  catch(Exception exc) {
	    		System.out.println(exc);
		  }
    }
    }
    catch(Exception e)
    {
    	
    }
  }
}